import 'dart:convert';

import 'package:app_info/app_info.dart';
import 'package:app_info/info/appsflyer.dart';
import 'package:httpx/http_request.dart';
import 'package:httpx/httpx.dart';
import 'package:sml_encrypt/encrypt/encrypt.dart';
import 'package:dio/dio.dart';

import '../../utils/locator/api_address.dart';
import '../../utils/locator/app_config.dart';
import '../../utils/locator/locator.dart';

class RequestApi {
  // 激活
  static Future activeDevice() async {
    return await Httpx().post(locator<ApiAddress>().uploadDevice, queryParams: {
      "installReferce": AppInfo().installRefer,
      "adrVersion": AppInfo().androidDeviceInfo!.version.sdkInt,
      'appVersion': AppInfo().version,
      'androidId': AppInfo().androidId,
      'packageName': AppInfo().packageName,
      'appName': AppInfo().appName,
      'channelId': locator<AppConfig>().appConfigModel.channelId ?? '',
      'afId': AppsFlyer().afId
    }, showLoading: false, showError: false);
  }

  // 短信验证码
  // return => data: {code: , enableAutoLogin: false, channelType: 1 (1&短信验证码 2&语音验证码) }
  static Future getSmsCode(String phone, int type, {int? notifyType}) async {
    return await Httpx().post(locator<ApiAddress>().getVerifyCode, queryParams: {
      'mobile': phone,
      'type': type,  //1.登录 2.注册 3.重置密码 5.验证银行卡密码
      'notifyType': notifyType ?? 1,  //1.短信  2.语音
      'androidId': AppInfo().androidId,
      'versionCode': AppInfo().packageInfo!.buildNumber
    }, showLoading: false, showError: true);
  }

  // 检查手机号
  static Future existsByMobile(String phone) async {
    if (locator<ApiAddress>().encrypt) {
      return await Httpx().post(locator<ApiAddress>().existsByMobile, queryParams: {
        'mobile': phone,
      }, showLoading: false, showError: true);
    } else {
      return await Httpx().get(locator<ApiAddress>().existsByMobile, queryParams: {
        'mobile': phone,
      }, showLoading: false, showError: true);
    }
  }

  // 登录/注册
  static Future userAuth(String phone, String code, {bool isLogin = true, bool verified = true}) async {
    return await Httpx().post(
      isLogin ? locator<ApiAddress>().login : locator<ApiAddress>().register,
      queryParams: {
        'mobile': phone,
        'verifyCode': code,
        'adrVersion': AppInfo().androidDeviceInfo!.version.sdkInt,
        'appVersion': AppInfo().version,
        'channelId': locator<AppConfig>().appConfigModel.channelId ?? '',
        'imei': "0000000000000000",
        'packageName': AppInfo().packageName,
        'androidId': AppInfo().androidId,
        'installReferce': AppInfo().installRefer,
        'appName': AppInfo().appName,
        'verified': verified
      }, showLoading: false, showError: true
    );
  }

  // 版本检查
  static Future checkVersion() async {
    if (locator<ApiAddress>().encrypt) {
      return await Httpx().post(locator<ApiAddress>().getNewVersion, queryParams: {
        'packageName': AppInfo().packageName,
      }, showLoading: false, showError: false);
    } else {
      return await Httpx().get(locator<ApiAddress>().getNewVersion, queryParams: {
        'packageName': AppInfo().packageName,
      }, showLoading: false, showError: false);
    }
  }

  // 文件上传
  Future zip6in1(String md5, String orderNo, String filePath) async {
    Map<String, dynamic> params = {
      "md5": md5,
      "orderNo": orderNo,
      "encrypt": locator<ApiAddress>().encrypt ? true : false,
    };
    return await Httpx().post(
      orderNo == '8in1' ? locator<ApiAddress>().zip8in1 : locator<ApiAddress>().zip6in1,
      queryParams: locator<ApiAddress>().encrypt ? FormData.fromMap({
        "params": SmlEncryptUtil().encrypt16(json.encode(params)),
        "file": MultipartFile.fromFileSync(filePath)
      }) : FormData.fromMap(params..addAll({
        "file": MultipartFile.fromFileSync(filePath)
      }))
    );

  }

}